//https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
package codeRandomThoughts.Test108将有序数组转换为二叉搜索树;

import codeRandomThoughts.utils.TreeNode;

public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        int start = 0;
        int end = nums.length - 1;
        return toBST(nums, start, end);
    }

    public TreeNode toBST(int[] nums, int start, int end) {
        //边界条件要注意
        if (start > end) {
            return null;
        }
        int mid = (start + end) / 2;

        TreeNode root = new TreeNode(nums[mid]);
        root.left = toBST(nums, start, mid - 1);
        root.right = toBST(nums, mid + 1, end);
        return root;
    }
}